#include "key_scan_cfg.h"
#include "key_process.h"
#include "n32g003_gpio.h"

#define KEY1_PORT             GPIOA
#define KEY1_PIN              GPIO_PIN_12
#define KEY2_PORT             GPIOA
#define KEY2_PIN              GPIO_PIN_10
#define KEY3_PORT             GPIOA
#define KEY3_PIN              GPIO_PIN_11
#define KEY4_PORT             GPIOA
#define KEY4_PIN              GPIO_PIN_4
#define KEY5_PORT             GPIOA
#define KEY5_PIN              GPIO_PIN_5
#define KEY6_PORT             GPIOA
#define KEY6_PIN              GPIO_PIN_14
#define KEY7_PORT             GPIOB
#define KEY7_PIN              GPIO_PIN_0




static void Key_Input_Initialize(GPIO_Module* GPIOx, uint16_t pin)
{
    /* Define a structure of type GPIO_InitType */
    GPIO_InitType GPIO_InitStructure;

    /* Enable KEY related GPIO peripheral clock */
    if(GPIOx == GPIOA)
    {
        RCC_APB_Peripheral_Clock_Enable(RCC_APB_PERIPH_IOPA);
    }
    else if(GPIOx == GPIOB)
    {
        RCC_APB_Peripheral_Clock_Enable(RCC_APB_PERIPH_IOPB);
    }
    else
    {
        /* ... */
    }

    if(pin < GPIO_PIN_ALL)
    {
        /* Assign default value to GPIO_InitStructure structure */
        GPIO_Structure_Initialize(&GPIO_InitStructure);

        GPIO_InitStructure.Pin       = pin;
        GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
        GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP;
        /* Initialize GPIO */
        GPIO_Peripheral_Initialize(GPIOx, &GPIO_InitStructure);
    }
}
// ==================================================================
/// @brief  : 按键驱动底层io配置
// ==================================================================
void Key_Init_Port(void)
{
	Key_Input_Initialize(GPIOA, KEY1_PIN | KEY2_PIN | KEY3_PIN | KEY4_PIN | KEY5_PIN | KEY6_PIN | KEY7_PIN);
	Key_Input_Initialize(GPIOB, KEY7_PIN);

}


// ==================================================================
/// @brief  : 获取当前有效按键索引值
///
/// @returns: 返回当前有效按键索引
// ==================================================================
key_u8_t Get_KeyIndex_Port(void)
{
    /** add your code below */
    uint8_t cnt = 0, value = 0;

    if (!GPIO_Input_Pin_Data_Get(KEY1_PORT, KEY1_PIN)) {
        cnt++, value = 1;
    }
    if (!GPIO_Input_Pin_Data_Get(KEY2_PORT, KEY2_PIN)) {
        cnt++, value = 2;
    }
    if (!GPIO_Input_Pin_Data_Get(KEY3_PORT, KEY3_PIN)) {
        cnt++, value = 3;
    }
    if (!GPIO_Input_Pin_Data_Get(KEY4_PORT, KEY4_PIN)) {
        cnt++, value = 4;
    }
    if (!GPIO_Input_Pin_Data_Get(KEY5_PORT, KEY5_PIN)) {
        cnt++, value = 5;
    }
    if (!GPIO_Input_Pin_Data_Get(KEY6_PORT, KEY6_PIN)) {
        cnt++, value = 6;
    }
    if (!GPIO_Input_Pin_Data_Get(KEY7_PORT, KEY7_PIN)) {
        cnt++, value = 7;
    }

    if (cnt > 1)
        value = 0x00;
    return value;

}

// ==================================================================
/// @brief  : 按键驱动时基获取
///
/// @returns: 返回ms时基
// ==================================================================
key_u32_t key_clk;
key_u32_t Get_Clk_Port(void)
{
    /** key_u16_t clk; */
    /** return clk; */
    return key_clk;
}
